package com.hui.li.controller;

import com.hui.li.entity.Book;
import com.hui.li.service.BookService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * @projectName: Spring-RestTemplate
 * @package: com.hui.li.controller
 * @className: BookController
 * @author: Hui
 * @date: 2021/12/23 20:59
 */
@RestController
@RequestMapping("hui")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("selectBook")
    public String fanAll() throws IOException {
        /**
         * 1.获取员工数据
         * 2. 创建workBook
         * 3. 创建sheet
         * 4. 创建行
         * 5. 创建每一行中的单元格
         * 6. 填充单元格的文字
         * 7. 设置单元格的样式(可选)
         * 8. 写入到本地
         */
        //1.获取员工数据
        List<Book> books = bookService.selectAll();
        //创建workBook
        Workbook workbook = new XSSFWorkbook();
        //创建sheet
        Sheet sheet = workbook.createSheet("图书管理表");
        //添加标题
        Row titleRow = sheet.createRow(0);
        //创建每一行的单元格并赋值
        List<String> strings = Arrays.asList("图书id", "图书名字", "作者名字", "一级分类id", "二级分类id", "图书描述", "图书网址");
        for (int i = 0; i < strings.size(); i++) {
            //给标题行赋值
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(strings.get(i));
        }
        //日期格式化
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < books.size(); i++) {
            //创建行
            Row row = sheet.createRow(i+1);
            Book book = books.get(i);
            //创建每一行中的单元格
            Cell idCell = row.createCell(0);
            //给单元格赋值
            idCell.setCellValue(book.getBookId());

            Cell BookNameCell = row.createCell(1);
            BookNameCell.setCellValue(book.getBookName());

            Cell AuthorNameCell = row.createCell(2);
            AuthorNameCell.setCellValue(book.getAuthorName());

            Cell FirstTypeIdCell = row.createCell(3);
            FirstTypeIdCell.setCellValue(book.getFirstTypeId());

            Cell SecondTypeIdCell = row.createCell(4);
            SecondTypeIdCell.setCellValue(book.getSecondTypeId());

            Cell DescriptionCell = row.createCell(5);
            DescriptionCell.setCellValue(book.getDescription());

            Cell ImgUrlCell = row.createCell(6);
            ImgUrlCell.setCellValue(book.getImgUrl());
        }
        //写入到本地
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\Book.xlsx");
        workbook.write(fileOutputStream);

        fileOutputStream.close();
        workbook.close();

        return "success";
    }
}
